import request from "@/util/api/request";

export default class MyProxy {
  constructor(api) {
    return new Proxy({}, {
      get(target, apiName, receiver) {
        let apiOpt = api[apiName]
        if (!apiOpt) {
          throw new Error("接口【" + apiName + "】的信息不存在")
        }
        let url = apiOpt.url
        let method = apiOpt.method

        return async function () {
          let promissRes = request[method](url, arguments)
          if (apiName === 'dl') {

            let res = await promissRes
            request.setToken(res.token)
            return res;
          }
          return promissRes
        }
      }
    });
  }
}